package com.example.demo.controller;


import com.example.demo.entity.*;
import com.example.demo.model.MD5Util;
import com.example.demo.model.result;
import com.example.demo.services.UserHeaderService;
import com.example.demo.services.test;
import com.example.demo.services.userLoginServices;
import com.example.demo.services.userSecServices;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class userLoginController {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private userLoginServices userLoginServices;

    @Autowired
    private UserHeaderService userHeaderService;

    @Autowired
    private userSecServices userSecService;


    @Autowired
    private test pageIn;

    @GetMapping("/seletall")
    public List<userLogin> selectall(){
        return userLoginServices.selectall();
    }

    @GetMapping("/selectpage")
    public result test(Integer size,Integer num){
        return result.success(pageIn.selectall(size, num));
    }


    @RequestMapping(value = "/login",method = {RequestMethod.POST})
    public result userLogin(@RequestBody Login login){
        logger.info("用户正在进行登录操作");
        //密码加密传输，后端数据也是加密密码，一 一对应
        String password= MD5Util.inputpass(login.getPassword(),"1a2b3c");
        userLogin test=userLoginServices.selectLogin(login.getLoginName(),password);
        if(test!=null){
            return result.success(test);
        }else {
            return result.fail(CodeMsg.NULL_ERROR);
        }
        
    }

    @RequestMapping(value = "/teacher",method = {RequestMethod.POST})
    public result teacherLogin(@RequestBody teacherLogin test){
        logger.info("老师正在进行登录操作");
        teacherLogin hello = userLoginServices.selectTeacher(test.getId(),test.getName());
        if(hello==null){
            return result.fail(CodeMsg.NULL_ERROR);
        }else {
            return result.success(hello);
        }
    }

    @GetMapping("/headerGet")
    public result headerGet(@Param("userId")String userId){
        logger.info("用户获取头像信息成功");
        UserHeader userHeader= userHeaderService.selectUser(userId);
        return result.success(userHeader);
    }



    @GetMapping("/getAllStudent")
    public result getAllStrudent(){
        logger.info("后台管理员获取所有学生数据成功");
        return result.success(userSecService.selectAll());
    }


    @PostMapping("/updateStudents")
    public result updateStudents(@Param("id")String id,@Param("name")String name,@Param("lifeCity")String lifeCity,@Param("loginName")String loginName,@Param("password")String password){
        logger.info("后台管理员正在修改用户信息");
        //MD5加密输入
        String passwordTest=null;
        if(password==null){
            passwordTest=null;
        }else {
            passwordTest=MD5Util.inputpass(password,"1a2b3c");
        }
        return result.success(userSecService.updateSec(id, name, lifeCity, loginName, passwordTest));
    }

    @GetMapping("/getAllReport")
    public result getAllReport(){
        logger.info("管理员正在查询所有数据");
        return result.success(userSecService.selectAllGli());
    }


    @GetMapping("/updatePassword")
    public result updatePassword(String userId,String password){
        logger.info("用户更改密码");
        String xiugaiPassword= MD5Util.inputpass(password,"1a2b3c4d");
        return result.success(userLoginServices.updatePassword(userId,xiugaiPassword));
    }







}
